<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>登录</title>

    <!-- Bootstrap -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
    <!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
    <![endif]-->
</head>

<body>
    <div class="container">
        <div class="row">
            <h2 class="text-center">请登录...</h2>
            <form onsubmit="return false" id="login" class="form-horizontal">
                <div class="form-group">
                    <label for="user_name" class="col-sm-2 control-label">姓名</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="user_name" placeholder="请输入姓名" required />
                    </div>
                </div>
                <div class="form-group">
                    <label for="password" class="col-sm-2 control-label">密码</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="password" placeholder="请输入密码" required />
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <input type="submit" id="login_handle" class="btn btn-primary pull-right" value="登录" />
                    </div>
                </div>
            </form>
        </div>
    </div>
</body>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
<script>
    $(function(){
        // 请求地址
        var login_url = './login.php';
        // 登录操作、存储 Token
        $("#login_handle").click(function(){
            var user_name = $("#user_name").val();
            var password = $("#password").val();
            if(!user_name){
                alert('请输入姓名');
                return;
            }
            if(!password) {
                alert('请输入密码');
                return;
            }
            $.ajax({
                url:login_url,
                type:"POST",
                data:{"user_name":user_name,"password":password},
                success:function(back){
                    var status = back.status;
                    var info = back.info;
                    if(!status){
                        alert(info);
                        return;
                    }
                    var token = back.token;
                    localStorage.setItem('token', token);
                    // 登录成功跳转到首页
                    window.location.href = "./index.html";
                    return;
                },
                error:function(){
                    alert('网络不稳定');
                }
            });
        });
    });
</script>

</html>